<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

const animationDuration = 6000;

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "400px"
    },
    barData: {
      default: {}
    },
    loading: {
      default: true
    }
  },
  data() {
    return {
      chart: null,
      options: "",
      tableData: []
    };
  },
  watch: {
    barData: {
      handler(newValue, oldValue) {
        console.log("oldValue", oldValue);
        console.log("newvalue", newValue);
        this.barData = newValue; //把新值赋值给我们的属性数据
        this.initChart(); //刷新echarts图表
      },
      deep: true
    }
  },
  mounted() {
    this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      let option = {
        // color: ["#a4d8c2", "#ffb980", "#5ab1ef", "#2ec7c9", "#5ab1ef"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            textStyle: {
              color: "#fff"
            }
          }
        },
        grid: {
          borderWidth: 0,
          top: 80,
          textStyle: {
            color: "#fff"
          }
        },
        legend: {
          x: "4%",
          bottom: "2%",
          textStyle: {
            fontSize: 16
          }
        },
        calculable: true,
        xAxis: [
          {
            type: "category",
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitArea: {
              show: false
            },
            axisLabel: {
              interval: 0
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0
            },
            splitArea: {
              show: false
            }
          }
        ],
        series: [
          {
            type: "bar",
            itemStyle: {
              normal: {
                color: "#66e0e3",
                // color: new echarts.graphic.LinearGradient(
                //   1,
                //   0,
                //   0,
                //   1,
                //   [
                //     {
                //       offset: 0,
                //       color: "#66e0e3" // 0% 处的颜色 紫
                //     },
                //     {
                //       offset: 1,
                //       color: "#d8d5f9" // 100% 处的颜色
                //     }
                //   ],
                //   false
                // ),
                label: {
                  show: true,
                  textStyle: {
                    color: "#fff"
                  },
                  position: "top",
                  formatter: function(p) {
                    return p.value > 0 ? p.value : "";
                  }
                }
              }
            },
            lineStyle: {
              width: 4,
              shadowColor: "#66e0e3",
              shadowOffsetY: 2,
              shadowBlur: 2,
              opacity: 0.5
            }
          },
          {
            type: "bar",
            itemStyle: {
              normal: {
                // color: new echarts.graphic.LinearGradient(
                //   1,
                //   0,
                //   0,
                //   1,
                //   [
                //     {
                //       offset: 0,
                //       color: "#ff9f7f" // 0% 处的颜色 蓝
                //     },
                //     {
                //       offset: 1,
                //       color: "#f0f6fe" // 100% 处的颜色
                //     }
                //   ],
                //   false
                // ),
                color: "#ff9f7f",
                barBorderRadius: 0,
                borderWidth: 1,
                label: {
                  show: true,
                  position: "top",
                  formatter: function(p) {
                    return p.value > 0 ? p.value : "";
                  }
                }
              }
            },
            lineStyle: {
              width: 4,
              shadowColor: "#ff9f7f",
              shadowOffsetY: 2,
              shadowBlur: 2,
              opacity: 0.5
            }
          }
        ]
      };
      this.chart.setOption(option);
      this.chart.setOption(this.barData);
    }
  }
};
</script>
<style>
table {
  /* cellspacing: 0; */
  *border-collapse: collapse; /* IE7 and lower */
  border-spacing: 0;
  width: 100%;
}
.bordered tr:hover {
  background: #fbf8e9;
  -o-transition: all 0.1s ease-in-out;
  -webkit-transition: all 0.1s ease-in-out;
  -moz-transition: all 0.1s ease-in-out;
  -ms-transition: all 0.1s ease-in-out;
  transition: all 0.1s ease-in-out;
}

.bordered th {
  padding: 7px;
  text-align: center;
  /* cellspacing: 0; */
}

.bordered td {
  padding: 7px;
  text-align: center;
  /* cellspacing: 0; */
}

.bordered th {
  background-image: -webkit-gradient(
    linear,
    left top,
    left bottom,
    from(#ebf3fc),
    to(#dce9f9)
  );
  background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: linear-gradient(top, #ebf3fc, #dce9f9);
}
.bordered td:first-child,
.bordered th:first-child {
  border-left: none;
}

.bordered tr:nth-of-type(2n) {
  background: #ffffff;
  cursor: pointer;
}
.bordered tr:nth-of-type(2n + 1) {
  background: #f7fafc;
  cursor: pointer;
}

.bordered tbody tr:hover {
  background: #fbf8e9;
  -o-transition: all 0.1s ease-in-out;
  -webkit-transition: all 0.1s ease-in-out;
  -moz-transition: all 0.1s ease-in-out;
  -ms-transition: all 0.1s ease-in-out;
  transition: all 0.1s ease-in-out;
}
</style>
